<template>
  <div class="main-content-container">
    <div ref="search" class="app-search">
      <el-row>
        <el-col :span="12" class="searchInput">
          <el-input v-model="custQuery" placeholder="姓名、手机号、编号" clearable class="input-with-select" @change="searchBtn">
            <el-button slot="append" icon="el-icon-search" @click="searchBtn" />
          </el-input>
        </el-col>
        <el-col :span="12" align="right" class="addBtnStyle">
          <el-button type="primary" @click="addReturnVisit">+ 增加回访</el-button>
        </el-col>
      </el-row>
      <el-row type="flex" justify="start" class="margin-top float-l">
        <!-- <el-col :span="15"> -->
        <Search :data="selections" :attribute="'plannedDate'" @change="handleTypeChange" @dateChange="handleDateChange" />
        <!-- </el-col> -->
        <!-- <el-col> -->
        <el-select v-model="query.returnVisitType" placeholder="请选择回访类型" class="el-select-y" @change="getList">
          <el-option v-for="item in orderTypeList" :key="item.id" :label="item.value" :value="item.value" />
        </el-select>
        <!-- </el-col> -->
        <!-- <el-col :span="15">
        </el-col> -->
        <!-- <el-col>
        </el-col> -->
      </el-row>
      <el-row class="margin-top">
        <el-col>
          <TypeSelection :data="datas" :default-value="1" :on-change="datasChange" class="TypeSelection" />
        </el-col>
      </el-row>
    </div>
    <div class="margin-top box-show" :style="'height:'+ (innerHeight + 80) + 'px'">
      <el-table :data="list" border :row-class-name="tableColor" :height="innerHeight">
        <el-table-column label="顾客" prop="customerName" :show-overflow-tooltip="true" />
        <el-table-column label="性别" prop="customerSex" :show-overflow-tooltip="true" />
        <el-table-column label="回访类型" prop="returnVisitType" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="关联内容" prop="relationContent" /> -->
        <el-table-column label="计划回访时间" prop="plannedReturnVisitDate" :show-overflow-tooltip="true" />
        <el-table-column label="计划回访人" prop="revisiterName" :show-overflow-tooltip="true" />
        <el-table-column label="状态" prop="status" :show-overflow-tooltip="true" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button v-if="row.status !== '已回访'" type="text" @click="startReturnVisit(row)">开始回访</el-button>
            <el-button v-if="row.status === '已回访'" type="text" @click="seeReturnVisit(row)">查看回访</el-button>
            <el-button v-if="row.status !== '已回访'" type="text" @click="modifyReturnVisit(row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-num="queryParams.page" :page-size="queryParams.pageSize" :on-change="handlePaginationChange" :total="total" />
    </div>
    <AddReturnVisit v-if="visible" :visible.sync="visible" :return-visit-type-list="orderTypeList" :current-row="currentRow" @handleClose="visible = false;currentRow={}" @handleSuccess="handleSuccess" />
    <SeeReturnVisit v-if="visibleDetails" :visible.sync="visibleDetails" :current-row="currentRowDetails" @handleClose="visibleDetails = false" />
    <StartReturnVisit v-if="statrVisible" :visible.sync="statrVisible" :current-row="startReturnVisitContent" @handleClose="statrVisible = false" @handleSuccess="startHnadle" />
  </div>
</template>
<script>
import { TypeSelection } from "@monkey.do/monkey";
import Search from "@/components/Search";
import { backUp } from "@/config";
import { tableColor, labelHeigh } from '@/utils/common.js';
import AddReturnVisit from "./addreturnVisit";
import SeeReturnVisit from "./seeReturnVisit";
import StartReturnVisit from "./startReturnVisit";
window.onbeforeunload = event => {
  window.scrollTo(0, 0); // 每次刷新，滚动条在页面最上面
};
export default {
  components: {
    Search,
    TypeSelection,
    AddReturnVisit,
    SeeReturnVisit,
    StartReturnVisit,
  },
  data() {
    return {
      queryParams: {
        page: 1,
        pageSize: this.$store.state.commonsettings.maxPageSize,
      },
      orderTypeList: [{ id: 0, value: "不限回访类型", label: "" }, { id: 1, value: "未上门回访", label: "未上门回访" }, { id: 2, value: "未成交回访", label: "未成交回访" }, { id: 3, value: "术前回访", label: "术前回访" }, { id: 4, value: "术后回访", label: "术后回访" }, { id: 5, value: "一般回访", label: "一般回访" }],
      tableColor,
      innerHeight: null,
      total: 0,
      visible: false,
      visibleDetails: false,
      reveal: false,
      selections: backUp,
      query: {
        status: "未回访",
      },
      datas: [
        { id: 0, name: '全部' },
        { id: 1, name: '未回访' },
        // { id: 2, name: '待退款' },
        { id: 2, name: '已回访' },
        { id: 3, name: '已过期' },
        { id: 4, name: '已补回访' },
        // { id: 4, name: '已退款' },
      ],
      list: [],
      currentRow: {},
      custQuery: '',
      currentRowDetails: {},
      startReturnVisitContent: {},
      statrVisible: false,
    };
  },
  computed: {
  },
  mounted() {
    this.getList();
    this.innerHeight = labelHeigh(this.$refs.search);
  },
  methods: {
    async getList() {
      const query = JSON.parse(JSON.stringify(this.query));
      if (!query.returnVisitType || query.returnVisitType === '不限回访类型') query.returnVisitType = undefined;
      if (query.status === "全部") query.status = undefined;
      if (!query.plannedDate) {
        const date = new Date();
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const beginTime = year + "-" + month + "-" + day + " 00:00:00";
        const endTime = year + "-" + month + "-" + day + " 23:59:59";
        query.timeType = "today";
        query.plannedDate = beginTime + "_" + endTime;
      }
      const res = await this.$api['data/returnVisit/returnVisit']({ ...this.queryParams, query: query, custQuery: this.custQuery });
      if (res.code !== 200) return;
      this.list = res.data.result;
      this.total = res.data.total;
    },
    handlePaginationChange(pagination) {
      this.queryParams = { page: pagination.pageNum, pageSize: pagination.pageSize };
      this.getList();
    },
    handleDateChange(item) {
      this.queryParams.page = 1;
      Object.assign(this.query, item);
      this.getList();
    },
    handleTypeChange(item) {
      this.queryParams.page = 1;
      Object.assign(this.query, item);
      this.getList();
    },
    datasChange(item) {
      this.queryParams.page = 1;
      this.$set(this.query, 'status', item.name);
      this.getList();
    },
    searchBtn() {
      this.queryParams.page = 1;
      this.getList();
    },
    addReturnVisit() {
      this.visible = true;
    },
    async seeReturnVisit(row) {
      const res = await this.$api['data/returnVisit/getReturnVisit']({ id: row.id });
      if (res.code !== 200) return this.$message.error(res.msg);
      this.currentRowDetails = res.data;
      this.visibleDetails = true;
    },
    handleSuccess() {
      this.visible = false;
      this.currentRow = {};
      this.queryParams.page = 1;
      this.getList();
    },
    async startReturnVisit(row) {
      const res = await this.$api['data/returnVisit/getReturnVisit']({ id: row.id });
      if (res.code !== 200) return this.$message.error(res.msg);
      this.startReturnVisitContent = JSON.parse(JSON.stringify(res.data));
      this.statrVisible = true;
    },
    startHnadle() {
      this.statrVisible = false;
      this.getList();
    },
    async modifyReturnVisit(row) {
      const res = await this.$api['data/returnVisit/getReturnVisit']({ id: row.id });
      if (res.code !== 200) return this.$message.error(res.msg);
      this.currentRow = JSON.parse(JSON.stringify(res.data));
      this.visible = true;
    },
  },
};
</script>
<style scoped lang="scss">
.margin-top{
    margin-top: 10px;
}
</style>
